package org.example.springbootdeptemp.controller;

import org.example.springbootdeptemp.dto.PageBean;
import org.example.springbootdeptemp.dto.PageResult;
import org.example.springbootdeptemp.pojo.Emp;
import org.example.springbootdeptemp.pojo.Result;
import org.example.springbootdeptemp.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class EmpController {
    @Autowired
    private EmpService empService;

    @PutMapping("/emps")
    public Result updateEmp(@RequestBody Emp emp) {
        empService.updateEmps(emp);
        return Result.success();
    }

    @DeleteMapping("/emps/{ids}")
    public Result deleteEmp(@PathVariable Integer[] ids) {
        empService.deleteEmps(ids);
        return Result.success();
    }

    @GetMapping ("/emps")
    public Result selectPageEmps(@RequestBody PageBean<Emp> emp) {
        PageResult<Emp> empPageResult = empService.selectPageEmps(emp);
        return Result.success(empPageResult);
    }

    @GetMapping ("/emps/{id}")
    public Result selectById(@PathVariable Integer id) {
        Emp emp = empService.selectEmpById(id);
        return Result.success(emp);
    }

    @PostMapping ("/emps")
    public Result inset(@RequestBody Emp emp) {
        empService.insertEmp(emp);
        return Result.success();
    }

    @GetMapping ("/emps/chajian")
    public Result getEmpByPage(@RequestBody PageBean<Emp> emp) {
        PageResult<Emp> empPageResult = empService.getEmpsByPage(emp);
        return Result.success(empPageResult);
    }
}
